const requsts = require("/util/requst");
const util = require("/util/util");
Page({
  data: {
      userId:"",
      msgArr:[],
      btnval:"已收货",
      isReturn:false,
  },
  jump(e){
    my.navigateTo({
       url: '/page/common/orderdetails/orderdetails?orderId='+e.currentTarget.dataset.orderId,
    });
  },
  onLoad(e){
    var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        console.log(res)
        that.setData({
          userId:res.data.userId,
        })
        that.init()
      },
      fail: function(res){
      }
    });
  },
  closeorder(e){
    var that=this
       my.confirm({
        title: '温馨提示',
        content: '您确定要关闭该订单吗?',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if(result.confirm){
            var param={
              userId:that.data.userId,
              orderId:e.currentTarget.dataset.orderId
            }
              my.request({
              url: requsts.url+"/order/closeOrder",
              method: 'POST',
              headers: {'content-type': 'application/x-www-form-urlencoded'},
              data:param,
              dataType: 'json',
              success: function(res) {
                console.log(res)
                if(res.data.code==0){
                    that.init()
                }else{
                  util.toastfn(res.data.msg)
                }
              },
              fail: function(res) {
                  util.toastfn(res.errorMessage)
              },
            })
          }
              
          
        },
      })
  },
  Received(e){
      var that=this
       my.confirm({
        title: '温馨提示',
        content: '商品已经收到吗?',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if(result.confirm){
            var param={
              userId:that.data.userId,
              orderId:e.currentTarget.dataset.orderId
            }
              my.request({
              url: requsts.url+"/order/alreadySigned",
              method: 'POST',
              headers: {'content-type': 'application/x-www-form-urlencoded'},
              data:param,
              dataType: 'json',
              success: function(res) {
                console.log(res)
                if(res.data.code==0){
                    that.init()
                    
                }else{
                  util.toastfn(res.data.msg)
                }
              },
              fail: function(res) {
                  util.toastfn(res.errorMessage)
              },
            })
          }
              
          
        },
      })
  },
  Returnfn(e){
      var that=this
       my.confirm({
        title: '温馨提示',
        content: '您确定要将商品寄回吗?',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if(result.confirm){
            var param={
              userId:that.data.userId,
              orderId:e.currentTarget.dataset.orderId
            }
              my.request({
              url: requsts.url+"/order/applySendBack",
              method: 'POST',
              headers: {'content-type': 'application/x-www-form-urlencoded'},
              data:param,
              dataType: 'json',
              success: function(res) {
                console.log(res)
                if(res.data.code==0){
                    that.init()
                }else{
                  util.toastfn(res.data.msg)
                }
              },
              fail: function(res) {
                  util.toastfn(res.errorMessage)
              },
            })
          }
          
        },
      })
  },
  init(){
    var param={
      userId:this.data.userId
    }
    var that=this
      my.request({
      url: requsts.url+"/order/getUserOrderList",
      method: 'POST',
      headers: {'content-type': 'application/x-www-form-urlencoded'},
      data:param,
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.code==0){
            that.setData({
              msgArr:res.data.data,
            })
            console.log(that.data.msgArr.length)
        }else{
          util.toastfn(res.data.msg)
        }
      },
      fail: function(res) {
          util.toastfn(res.errorMessage)
      },
    });
  },
});